﻿<template>
  <div class="xuanKe-container">
    <el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
      <el-form :model="queryParams" ref="queryForm" labelWidth="90">
        <el-row>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
            <el-form-item label="选课任务">
              <el-select clearable="" filterable="" v-model="queryParams.xuankeTaskId" placeholder="请选择选课任务">
                <el-option v-for="(item,index) in  xuankeTaskXuankeTaskIdDropdownList" :key="index" :value="item.value" :label="item.label" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10">
            <el-form-item label="关键字">
              <el-input v-model="queryParams.searchKey" clearable="" placeholder="请输入模糊查询关键字"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="班级">
              <el-select clearable="" filterable="" v-model="queryParams.banjiId" placeholder="请选择班级">
                <el-option v-for="(item,index) in  banjiBanjiIdDropdownList" :key="index" :value="item.value" :label="item.label" />
                
              </el-select>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="教学班">
              <el-select clearable="" filterable="" v-model="queryParams.jiaoxueBanId" placeholder="请选择教学班">
                <el-option v-for="(item,index) in  jiaoxueBanJiaoxueBanIdDropdownList" :key="index" :value="item.value" :label="item.label" />
                
              </el-select>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="学生">
              <el-select clearable="" filterable="" v-model="queryParams.studentId" placeholder="请选择学生">
                <el-option v-for="(item,index) in  studentStudentIdDropdownList" :key="index" :value="item.value" :label="item.label" />
                
              </el-select>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="课程">
              <el-select clearable="" filterable="" v-model="queryParams.kechengId" placeholder="请选择课程">
                <el-option v-for="(item,index) in  kechengKechengIdDropdownList" :key="index" :value="item.value" :label="item.label" />
                
              </el-select>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="成绩">
              <el-input v-model="queryParams.chengji" clearable="" placeholder="请输入成绩"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="8" :xl="4" class="mb10" v-if="showAdvanceQueryUI">
            <el-form-item label="备注">
              <el-input v-model="queryParams.remark" clearable="" placeholder="请输入备注"/>
              
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="6" :xl="6" class="mb10">
            <el-form-item>
              <el-button-group>
                <el-button type="primary"  icon="ele-Search" @click="handleQuery" v-auth="'xuanKe:page'"> 查询 </el-button>
                <el-button icon="ele-Refresh" @click="() => queryParams = {}"> 重置 </el-button>
                <el-button icon="ele-ZoomIn" @click="changeAdvanceQueryUI" v-if="!showAdvanceQueryUI"> 高级 </el-button>
                <el-button icon="ele-ZoomOut" @click="changeAdvanceQueryUI" v-if="showAdvanceQueryUI"> 隐藏 </el-button>
                
              </el-button-group>
              
              <el-button-group style="margin-left:20px">
                <el-button type="primary" icon="ele-Plus" @click="openAddXuanKe" v-auth="'xuanKe:add'"> 新增 </el-button>
                <el-button icon="ele-FolderOpened" @click="exportXuankeHandle" v-auth="'xuanKe:export'"> 导出 </el-button>
              </el-button-group>
              
            </el-form-item>
            
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card class="full-table" shadow="hover" style="margin-top: 8px">
      <el-table
				:data="tableData"
				style="width: 100%"
				v-loading="loading"
				tooltip-effect="light"
				row-key="id"
				border="">
        <el-table-column type="index" label="序号" width="55" align="center"/>
        <el-table-column prop="xuankeTaskId" label="选课任务" width="120" show-overflow-tooltip="">
          <template #default="scope">
            <span>{{scope.row.xuankeTaskIdName}}</span>
            
          </template>
          
        </el-table-column>
        <el-table-column prop="banjiId" label="班级" width="120" show-overflow-tooltip="">
          <template #default="scope">
            <span>{{scope.row.banjiIdName}}</span>
            
          </template>
          
        </el-table-column>
        <el-table-column prop="jiaoxueBanId" label="教学班" width="120" show-overflow-tooltip="">
          <template #default="scope">
            <span>{{scope.row.jiaoxueBanIdName}}</span>
          </template>
        </el-table-column>

        <el-table-column prop="studentId" label="学号" width="120" show-overflow-tooltip="">
          <template #default="scope">
            <span>{{scope.row.studentIdXh}}</span>
          </template>
        </el-table-column>
        
        <el-table-column prop="studentId" label="学生" width="120" show-overflow-tooltip="">
          <template #default="scope">
            <span>{{scope.row.studentIdName}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="kechengId" label="课程" width="120" show-overflow-tooltip="">
          <template #default="scope">
            <span>{{scope.row.kechengIdName}}</span>
            
          </template>
          
        </el-table-column>
        <el-table-column prop="isYuxuan" label="是否预选" width="120" show-overflow-tooltip="">
          <template #default="scope">
            <el-tag v-if="scope.row.isYuxuan"> 是 </el-tag>
            <el-tag type="danger" v-else> 否 </el-tag>
            
          </template>
          
        </el-table-column>
        <el-table-column prop="chengji" label="成绩" width="140" show-overflow-tooltip="" />
        <el-table-column prop="remark" label="备注" width="140" show-overflow-tooltip="" />
        <el-table-column label="操作" width="140" align="center" fixed="right" show-overflow-tooltip="" v-if="auth('xuanKe:edit') || auth('xuanKe:delete')">
          <template #default="scope">
            <el-button icon="ele-Edit" size="small" text="" type="primary" @click="openEditXuanKe(scope.row)" v-auth="'xuanKe:edit'"> 编辑 </el-button>
            <el-button icon="ele-Delete" size="small" text="" type="primary" @click="delXuanKe(scope.row)" v-auth="'xuanKe:delete'"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
				v-model:currentPage="tableParams.page"
				v-model:page-size="tableParams.pageSize"
				:total="tableParams.total"
				:page-sizes="[10, 20, 50, 100, 200, 500]"
				small=""
				background=""
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				layout="total, sizes, prev, pager, next, jumper"
	/>
      <printDialog
        ref="printDialogRef"
        :title="printXuanKeTitle"
        @reloadTable="handleQuery" />
      <editDialog
        ref="editDialogRef"
        :title="editXuanKeTitle"
        @reloadTable="handleQuery"
      />
    </el-card>
  </div>
</template>

<script lang="ts" setup="" name="xuanKe">
  import { ref } from "vue";
  import { ElMessageBox, ElMessage } from "element-plus";
  import { auth } from '/@/utils/authFunction';
  import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
  import { formatDate } from '/@/utils/formatTime';

  import printDialog from '/@/views/system/print/component/hiprint/preview.vue'
  import editDialog from '/@/views/main/xuanKe/component/editDialog.vue'
  import { pageXuanKe, deleteXuanKe,exportXuanke } from '/@/api/main/xuanKe';
  import { getXuankeTaskXuankeTaskIdDropdown } from '/@/api/main/xuanKe';
  import { getBanjiBanjiIdDropdown } from '/@/api/main/xuanKe';
  import { getJiaoxueBanJiaoxueBanIdDropdown } from '/@/api/main/xuanKe';
  import { getStudentStudentIdDropdown } from '/@/api/main/xuanKe';
  import { getKechengKechengIdDropdown } from '/@/api/main/xuanKe';

  import { downloadByUrl, downloadByData, getFileName } from '/@/utils/download';

  const showAdvanceQueryUI = ref(false);
  const printDialogRef = ref();
  const editDialogRef = ref();
  const loading = ref(false);
  const tableData = ref<any>([]);
  const queryParams = ref<any>({});
  const tableParams = ref({
    page: 1,
    pageSize: 10,
    total: 0,
  });

  const printXuanKeTitle = ref("");
  const editXuanKeTitle = ref("");

  // 改变高级查询的控件显示状态
  const changeAdvanceQueryUI = () => {
    showAdvanceQueryUI.value = !showAdvanceQueryUI.value;
  }
  

  // 查询操作
  const handleQuery = async () => {
    loading.value = true;
    var res = await pageXuanKe(Object.assign(queryParams.value, tableParams.value));
    tableData.value = res.data.result?.items ?? [];
    tableParams.value.total = res.data.result?.total;
    loading.value = false;
  };

  // 打开新增页面
  const openAddXuanKe = () => {
    editXuanKeTitle.value = '添加校本选课';
    editDialogRef.value.openDialog({});
  };

  // 打开打印页面
  const openPrintXuanKe = async (row: any) => {
    printXuanKeTitle.value = '打印校本选课';
  }
  
  // 打开编辑页面
  const openEditXuanKe = (row: any) => {
    editXuanKeTitle.value = '编辑校本选课';
    editDialogRef.value.openDialog(row);
  };

  // 删除
  const delXuanKe = (row: any) => {
    ElMessageBox.confirm(`确定要删除吗?`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
  .then(async () => {
    await deleteXuanKe(row);
    handleQuery();
    ElMessage.success("删除成功");
  })
  .catch(() => {});
  };

  // 改变页面容量
  const handleSizeChange = (val: number) => {
    tableParams.value.pageSize = val;
    handleQuery();
  };

  // 改变页码序号
  const handleCurrentChange = (val: number) => {
    tableParams.value.page = val;
    handleQuery();
  };

  const xuankeTaskXuankeTaskIdDropdownList = ref<any>([]); 
  const getXuankeTaskXuankeTaskIdDropdownList = async () => {
    let list = await getXuankeTaskXuankeTaskIdDropdown();
    xuankeTaskXuankeTaskIdDropdownList.value = list.data.result ?? [];
  };
  getXuankeTaskXuankeTaskIdDropdownList();
  
  const banjiBanjiIdDropdownList = ref<any>([]); 
  const getBanjiBanjiIdDropdownList = async () => {
    let list = await getBanjiBanjiIdDropdown();
    banjiBanjiIdDropdownList.value = list.data.result ?? [];
  };
  getBanjiBanjiIdDropdownList();
  
  const jiaoxueBanJiaoxueBanIdDropdownList = ref<any>([]); 
  const getJiaoxueBanJiaoxueBanIdDropdownList = async () => {
    let list = await getJiaoxueBanJiaoxueBanIdDropdown();
    jiaoxueBanJiaoxueBanIdDropdownList.value = list.data.result ?? [];
  };
  getJiaoxueBanJiaoxueBanIdDropdownList();
  
  const studentStudentIdDropdownList = ref<any>([]); 
  const getStudentStudentIdDropdownList = async () => {
    let list = await getStudentStudentIdDropdown();
    studentStudentIdDropdownList.value = list.data.result ?? [];
  };
  getStudentStudentIdDropdownList();
  
  const kechengKechengIdDropdownList = ref<any>([]); 
  const getKechengKechengIdDropdownList = async () => {
    let list = await getKechengKechengIdDropdown();
    kechengKechengIdDropdownList.value = list.data.result ?? [];
  };
  
  const exportXuankeHandle = async () => {
    if(queryParams.value.xuankeTaskId === undefined){
      ElMessage.error('选课任务必选');
      return ;
    }
    loading.value = true;
    var res = await exportXuanke(Object.assign(queryParams.value, tableParams.value));
    loading.value = false;
    var fileName = getFileName(res.headers);
    downloadByData(res.data as any, fileName);
  };
  handleQuery();
</script>
<style scoped>
:deep(.el-ipnut),
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>

